<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <link type="text/css" href="styles/base/ui.all.css" rel="stylesheet" />
    <style type="text/css" media="screen">
        @import url("styles/gb.css");
		@import url("styles/autocomplete/jquery.autocomplete.css");
    </style>
    <script type="text/javascript" src="scripts/jquery.min.js "></script>
    <script type="text/javascript" src="scripts/jquery.parsequery.js" ></script>
    <script type="text/javascript" src="scripts/jquery-jtemplates.js"></script>
    <script type="text/javascript" src="scripts/jquery.corner.js"></script>
    <script type="text/javascript" src="scripts/ui/jquery-ui-1.7.2.custom.js"></script>
    <script type="text/javascript" src="scripts/ui/ui.datepicker.js"></script>
    <script type="text/javascript" src="scripts/flowplayer/flowplayer-3.1.1.min.js"></script>
    <script type="text/javascript" src="scripts/date.js"></script>
	<script type="text/javascript" src="scripts/jquery.autocomplete.min.js"></script>
	<script type="text/javascript" src="scripts/navigation.js"></script>
    <script type="text/javascript" src="scripts/jquery.easing.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.lavalamp.min.js"></script>
	<script>
        var query_vars;
		var db_name;
        $(function() {
           query_vars = $.parseQuery();
		   db_name = findDBName();

           $.getJSON("/"+db_name+"/_design/tags/_view/by_tag_grouped?group=true&callback=?", function(data){
				// attach the template
				$("#list_table").setTemplateElement("list_table_template");
			
				// process the template
				$("#list_table").processTemplate(data);
           });
		   
		   
		   $('#removeTagDialog').dialog({
			    	autoOpen: false,
			    	resizable: false,
		            modal: true,
					buttons: {
						'Ok': function() {
						   $(this).dialog('close');
						   $.getJSON("/" + db_name + "/_design/tags/_view/by_tag?key=%22"+ tag_to_remove +"%22&callback=?", function(data){
								jQuery.each(data.rows, function(i, val) {
								    var tag_id = this.value._id;
									var tag_rev = this.value._rev;
									//console.log('delete ' + this.value._id + " rev: " + this.value._rev);
									$.ajax({
									   type: "DELETE",
									   url: "/" + db_name + "/" + tag_id + "?rev=" + tag_rev,
									   success: function(msg){
										  //console.log("successful delete: " + tag_id);
									   }
									});
									$('#row-' + tag_to_remove).hide();
								});
							});
						},
						'Cancel' : function() {
							$(this).dialog('close');
						}
					},
					close: function() {}
		    });
		   
		   
        });
		
		var tag_to_remove;
		function deleteTag(tag) {
			tag_to_remove = tag;
		    $('#removeTagDialog').dialog('open');
			return false;
		}
		
    </script>
  </head>
  <body class="primary-4">
	<div id="header"></div>
  
    <div id="content" data-corner="left right 20px">
	   <div id="list_table" class="jTemplatesTest"></div>
    </div>
	<!-- Template content (Valid XHTML 1.1) -->
	<p style="display: none;"><textarea id="list_table_template" rows="0" cols="0">
		<table>
			<tr>
			  <td>Tag</td>
			  <td>Count</td>
			  <td>Actions</td>
			</tr>
			{#foreach $T.rows as row}
                        <tr id="row-{$T.row.key}">
                            <td><a href="tag.html?tag={$T.row.key}">{$T.row.key}</a></td><td>{$T.row.value}</td>
							<td>
							   <a href="#" onclick="return deleteTag('{$T.row.key}');"><img src="images/cross-light.png"   title="Delete all instances of this tag." alt="Delete all instances of this tag."      onmouseover="this.src='images/cross.png'"  onmouseout="this.src='images/cross-light.png'" class="noborder"/></a>
							</td>
                        </tr>
			{#/for}
		</table>
	</textarea></p>
    <!-- End Of template-->

	<div id="removeTagDialog" style="display: none;" title="Remove Tags...">
		Are you sure you want to remove all instances of this tag?
	</div>
  </body>
</html>
